package com.starsea.book.ui.newpage

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentWalletCashBinding
import com.starsea.book.ui.newpage.activity.WithdrawalActivity
import com.starsea.book.ui.newpage.adapter.WalletDetailsAdapter
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-02-23 10 GMT+8
 * @desc :
 */
class WalletCashFragment : VMBaseFragment<WalletViewModel>(R.layout.fragment_wallet_cash) {

    private val binding by viewBinding(FragmentWalletCashBinding::bind)
    override val viewModel by viewModels<WalletViewModel>()
    var page = 1
    lateinit var mAdapter: WalletDetailsAdapter

    companion object {
        fun newInstance(): WalletCashFragment {
            val args = Bundle()

            val fragment = WalletCashFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        mAdapter = WalletDetailsAdapter(requireContext(), 1) {

        }

        binding.rtvWithdraw.setOnClickListener {
            startActivity<WithdrawalActivity> { }
        }


        binding.rvContent.layoutManager = LinearLayoutManager(requireContext())
        binding.rvContent.adapter = mAdapter

        viewModel.walletInfoResult.observe(this) { data ->
            if (data.status == 200) {

                binding.tvNum.text = (data.data!!.rmbBalance ?: "0").toDouble().finialDouble()

                binding.tvTodayCoinNum.text =
                    (data.data!!.todayCash ?: "0").toDouble().finialDouble()
                binding.tvTotalNum.text =
                    (data.data!!.accumulatedRmb ?: "0").toDouble().finialDouble()

            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                toastOnUi(data.msg)
            }
        }



        binding.srlRefresh.setOnRefreshListener {
            page = 1
            viewModel.getWalletDetails(2, page)
        }
        binding.srlRefresh.setOnLoadMoreListener {
            page++
            viewModel.getWalletDetails(2, page)
        }


        viewModel.getWalletDetails(2, page)


        viewModel.walletDetailsResult.observe(this) { data ->
            if (data.status == 200) {

                if (page == 1) {
                    binding.srlRefresh.finishRefresh()
                    mAdapter.setItems(data.data!!.list)
                } else {
                    binding.srlRefresh.finishLoadMore()
                    mAdapter.addItems(data.data!!.list!!)
                }
                if (data.data!!.list?.size!! < 10) {
                    binding.srlRefresh.finishLoadMoreWithNoMoreData()
                }

            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(requireContext())
            } else {
                toastOnUi(data.msg)
            }
        }

    }


    override fun onResume() {
        super.onResume()
        viewModel.getWalletInfo()
    }
}